//
//  UIfaceimageview.m
//  cherryproduct
//
//  Created by umessage on 14-8-27.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "UIfaceimageview.h"

@implementation UIfaceimageview
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc] init];
        [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
        //注册
        UICollectionView* collectionview = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        [collectionview registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        collectionview.delegate = self;
        collectionview.dataSource  = self;
        collectionview.backgroundColor = [UIColor whiteColor];
        [self addSubview:collectionview]; //表情列表

    }
    return self;
}


#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 135;
}
//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* string = @"cell";
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:string forIndexPath:indexPath];
    
    if([cell viewWithTag:101])
    {
        UIImageView* imageview = (UIImageView*)[cell viewWithTag:101];
        imageview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.gif",indexPath.row]];
    }
    else
    {
        UIImageView* imageview= [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width/7, self.frame.size.width/7)];
        imageview.image =[UIImage imageNamed:[NSString stringWithFormat:@"%d.gif",indexPath.row]];
        [cell addSubview:imageview];
    }
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}


#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.frame.size.width/7, self.frame.size.width/7);
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0,0);
}
#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate UIfaceimageviewdelegate:indexPath.row];
    NSLog(@"face%d",indexPath.row);
    
}
//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end




@interface UIView_Select_Star ()

@property(nonatomic, strong) UIButton * btn_0;
@property(nonatomic, strong) UIButton * btn_1;
@property(nonatomic, strong) UIButton * btn_2;
@property(nonatomic, strong) UIButton * btn_3;
@property(nonatomic, strong) UIButton * btn_4;

@end

@implementation UIView_Select_Star

- (id)initWithFrame:(CGRect)frame withTitle:(NSString *)title
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        CGRect rect = CGRectMake(10, 0, 120, 25);
        UILabel * label_title = [[UILabel alloc] initWithFrame:rect];
        label_title.backgroundColor=[UIColor clearColor];
        label_title.textColor=[UIColor grayColor];
        label_title.font=[UIFont systemFontOfSize:14];
        label_title.text = title;
        [self addSubview:label_title];
        
        UIImage * image_select_yes = [UIImage imageNamed:@"productreviewsup"];
        UIImage * image_select_no  = [UIImage imageNamed:@"productreviewsdown"];
        
        int sa_width = 110;
        for (int i = 0; i < 5; i++) {
            UIButton * btn_bb = [[UIButton alloc] initWithFrame:CGRectMake(sa_width, 0, 25, 25)];
            [btn_bb addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn_bb setImage:image_select_no forState:UIControlStateNormal];
            [btn_bb setImage:image_select_yes forState:UIControlStateSelected];
            [btn_bb setImage:image_select_yes forState:UIControlStateHighlighted];
            [self addSubview:btn_bb];
            
            btn_bb.selected=YES;
            if (i == 4) {
                btn_bb.selected=NO;
            }
            
            sa_width += 30;
            
            btn_bb.tag=i;
            switch (i) {
                case 0:
                {
                    self.btn_0=btn_bb;
                }
                    break;
                case 1:
                {
                    self.btn_1=btn_bb;
                }
                    break;
                case 2:
                {
                    self.btn_2=btn_bb;
                }
                    break;
                case 3:
                {
                    self.btn_3=btn_bb;
                }
                    break;
                case 4:
                {
                    self.btn_4=btn_bb;
                }
                    break;
                    
                default:
                    break;
            }
        }
        
    }
    return self;
}

- (void)doAction:(UIButton*)sender
{
    if (self.delegate_click) {
        [self.delegate_click doGroupIndex:self.tag starIndex:sender.tag+1];
    }
    NSInteger index = sender.tag;
    switch (index) {
        case 0:
        {
            self.btn_0.selected=YES;
            self.btn_1.selected=NO;
            self.btn_2.selected=NO;
            self.btn_3.selected=NO;
            self.btn_4.selected=NO;
        }
            break;
        case 1:
        {
            self.btn_0.selected=YES;
            self.btn_1.selected=YES;
            self.btn_2.selected=NO;
            self.btn_3.selected=NO;
            self.btn_4.selected=NO;
        }
            break;
        case 2:
        {
            self.btn_0.selected=YES;
            self.btn_1.selected=YES;
            self.btn_2.selected=YES;
            self.btn_3.selected=NO;
            self.btn_4.selected=NO;
        }
            break;
        case 3:
        {
            self.btn_0.selected=YES;
            self.btn_1.selected=YES;
            self.btn_2.selected=YES;
            self.btn_3.selected=YES;
            self.btn_4.selected=NO;
        }
            break;
        case 4:
        {
            self.btn_0.selected=YES;
            self.btn_1.selected=YES;
            self.btn_2.selected=YES;
            self.btn_3.selected=YES;
            self.btn_4.selected=YES;
        }
            break;
            
        default:
            break;
    }
}

@end




@implementation UIView_PXO_Face

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        // Initialization code
        UIButton * btn_face = nil;
        CGFloat top = 20;
        CGFloat width = frame.size.width;
        CGFloat fen = (width-20)/6;
        CGFloat left = 10;
        
        for (int i = 0; i < 6; i++) {
            btn_face = [[UIButton alloc] initWithFrame:CGRectMake(left, top, fen, 40)];
            NSString * str_f = [NSString stringWithFormat:@"face%d", i];
            btn_face.tag=i;
            [btn_face setImage:[UIImage imageNamed:str_f] forState:UIControlStateNormal];
            [btn_face addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn_face];
            
            left += fen;
        }
        
        left = 10;
        top = 70;
        
        for (int i = 6; i < 12; i++) {
            btn_face = [[UIButton alloc] initWithFrame:CGRectMake(left, top, fen, 40)];
            NSString * str_f = [NSString stringWithFormat:@"face%d", i];
            btn_face.tag=i;
            [btn_face setImage:[UIImage imageNamed:str_f] forState:UIControlStateNormal];
            [btn_face addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn_face];
            
            left += fen;
        }
        
        left = 10;
        top = 120;
        
        for (int i = 12; i < 18; i++) {
            btn_face = [[UIButton alloc] initWithFrame:CGRectMake(left, top, fen, 40)];
            NSString * str_f = [NSString stringWithFormat:@"face%d", i];
            btn_face.tag=i;
            [btn_face setImage:[UIImage imageNamed:str_f] forState:UIControlStateNormal];
            [btn_face addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn_face];
            
            left += fen;
        }
    }
    return self;
}

- (void)doAction:(UIButton*)sender
{
    if (self.delegate_click) {
        NSString * str_face = [NSString stringWithFormat:@"[qqface]%d[/qqface]", sender.tag];
        [self.delegate_click doClickFaceIndex:str_face];
    }
}

@end
























